package grabber;

import data.DataHelper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;

public class TyresGrabber {

    public static final String BASE_URL = "http://www.autoboots.ru";
    public static final String IMAGES_PATH = "data/tyres/images/";
    public static final String DATA_PATH = "data/tyres/";
    public static final boolean GRAB_PICS = false;

    public static void main(String[] args) throws IOException {
        String vendors[] = new String[]{
                "Continental",
                "Nokian",
                "Pirelli",
                "Dunlop",
                "Gislaved",
                "Kleber",
                "Kumho",
                "Matador",
                "Michelin",
                "Nordman",
                "Yokohama",
                "Barum",
                "BFGoodrich",
                "Roadstone",
                "Sava",
                "Tigar"
        };

        for (String vendor : vendors) {
            File dataFile = new File(DATA_PATH + vendor + ".tyres.txt");
            PrintStream out = new PrintStream(new FileOutputStream(dataFile));
            String url = BASE_URL + "/tyresbp/tprc-" + vendor;
            Document doc = Jsoup.connect(url).timeout(20000).get();
            Elements elems = doc.select("table.catalog-list tr");
            for (Element elem : elems) {
                String href = elem.select("td a.tooltip_show").attr("href");
                if (href.length() != 0) {
                    String model = elem.select("a.tooltip_show").text();
                    model = model.replace(vendor.toUpperCase(), "").trim();
                    model = model.substring(1, model.length());
                    Tyre tyre = new Tyre();
                    tyre.vendor = vendor;
                    tyre.model = model;
                    tyre.typesize = elem.child(2).text().replace("R", " R");
                    tyre.season = elem.child(4).text();
                    tyre.speedIdx = elem.child(5).text().replace((char) 160, (char) 32).trim();
                    tyre.loadIdx = elem.child(6).text().replace((char) 160, (char) 32).trim();
                    String price = elem.child(10).text();
                    price = price.replace((char) 160, (char) 32);
                    price = DataHelper.codeInURL(price);
                    price = price.replace(".00", "");
                    tyre.price = (Integer.parseInt(price) / 100) * 100;
                    model = model.replace("/", "-");
                    if (GRAB_PICS) {
                        url = BASE_URL + href;
                        doc = Jsoup.connect(url).timeout(20000).get();
                        String imgUrl = BASE_URL + doc.select("table.catalog-list tr:gt(3) a").attr("href");
                        BufferedImage img = ImageIO.read(new URL(imgUrl));
                        if (img == null) {
                            System.out.println("No Image: " + vendor + model);
                        } else {
                            ImageIO.write(img, "jpg", new File(IMAGES_PATH + vendor + " " + model + ".jpg"));
                        }
                    }
                    tyre.imageSmall = vendor + model + "-small.jpg";
                    tyre.imageMedium = vendor + model + "-medium.jpg";
                    tyre.imageLarge = vendor + model + "-large.jpg";
                    out.println(tyre);
                }
            }
            System.out.println(elems.size());
        }
    }
}
